﻿CREATE PROCEDURE [branding].[GetAllObjects]
	@tag varchar(50),
	@subTag varchar(50),
	@deferredLoadingLength int
AS
	select	bb.ID,
			'blob' as ObjectType,
			bb.Tag,
			bb.SubTag,
			bb.ResourceId,
			bb.Brand,
			bb.ContentLength,
			bb.MimeType,
			null as Blob,
			bb.BlobGuid,
			null as [Text],
			null as TypeName,
			null as SerializedValue
	from	branding.Blob as bb
	where	(@tag is null or @tag = Tag) and
			(@subTag is null or @subTag = SubTag)

	union
	select	bt.ID,
			'text' as ObjectType,
			bt.Tag,
			bt.SubTag,
			bt.ResourceId,
			bt.Brand,
			bt.ContentLength,
			null as MimeType,
			null as Blob,
			null as BlobGuid,
			case
				when LEN(bt.[Text]) >= @deferredLoadingLength then null
				else bt.[Text]
			end as [Text],
			null as TypeName,
			null as SerializedValue
	from	branding.[Text] as bt
	where	(@tag is null or @tag = Tag) and
			(@subTag is null or @subTag = SubTag)

	union
	select	bv.ID,
			'value' as ObjectType,
			bv.Tag,
			bv.SubTag,
			bv.ResourceId,
			bv.Brand,
			bv.ContentLength,
			null as MimeType,
			null as Blob,
			null as BlobGuid,
			null as [Text],
			bv.TypeName as TypeName,
			case
				when LEN(bv.SerializedValue) >= @deferredLoadingLength then null
				else bv.SerializedValue
			end as SerializedValue
	from	branding.Value as bv
	where	(@tag is null or @tag = Tag) and
			(@subTag is null or @subTag = SubTag)

	order   by ResourceId;
RETURN 0